package com.example.entry;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class RequestHistory {
    private String name;
    private String method;
    private String url;
    private String uuid;
    private List<HeaderVo> headers = new ArrayList<>();
    private List<HeaderVo> cookies = new ArrayList<>();
    private Boolean formDataType =false;
    private List<BodyVo> formDataEntries = new ArrayList<>();
    private String rawData;
    private String rawType;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getUuid() {
        return uuid;
    }

    public void setUuid(String uuid) {
        this.uuid = uuid;
    }

    public List<HeaderVo> getHeaders() {
        return headers;
    }

    public void setHeaders(List<HeaderVo> headers) {
        this.headers = headers;
    }

    public List<HeaderVo> getCookies() {
        return cookies;
    }

    public void setCookies(List<HeaderVo> cookies) {
        this.cookies = cookies;
    }

    public Boolean getFormDataType() {
        return formDataType;
    }

    public void setFormDataType(Boolean formDataType) {
        this.formDataType = formDataType;
    }

    public List<BodyVo> getFormDataEntries() {
        return formDataEntries;
    }

    public void setFormDataEntries(List<BodyVo> formDataEntries) {
        this.formDataEntries = formDataEntries;
    }

    public String getRawData() {
        return rawData;
    }

    public void setRawData(String rawData) {
        this.rawData = rawData;
    }

    public String getRawType() {
        return rawType;
    }

    public void setRawType(String rawType) {
        this.rawType = rawType;
    }

    public void setFormDataEntriesEntry(List<BodyEntry> bodyEntries) {
        if (CollectionUtil.isEmpty(bodyEntries)) {
            this.formDataEntries=null;
        }else {
            List<BodyVo> formDataEntries = new ArrayList<>();
            for (BodyEntry body : bodyEntries) {
                BodyVo vo=new BodyVo();
                if (StrUtil.isEmpty(body.getName())){
                    continue;
                }
                vo.setType(body.getType());
                vo.setName(body.getName());

                Object value = body.getValue();
                if (value instanceof File file){
                    vo.setFullPath(file.getAbsolutePath());
                }else{
                    vo.setValue(value);
                }
                formDataEntries.add(vo);
            }
            this.formDataEntries=formDataEntries;
        }
    }
    public void setCookiesEntry(List<HeaderEntry> headers) {
        if (CollectionUtil.isEmpty(headers)) {
            this.cookies=null;
        }else {
            List<HeaderVo> headerVos = new ArrayList<>();
            for (HeaderEntry header : headers) {
                HeaderVo vo=new HeaderVo();
                if (StrUtil.isEmpty(header.getName())){
                    continue;
                }
                vo.setName(header.getName());
                vo.setValue(header.getValue());
                headerVos.add(vo);
            }
            this.cookies=headerVos;
        }
    }
    public void setHeadersEntry(List<HeaderEntry> headers) {
        if (CollectionUtil.isEmpty(headers)) {
            this.headers=null;
        }else {
            List<HeaderVo> headerVos = new ArrayList<>();
            for (HeaderEntry header : headers) {
                HeaderVo vo=new HeaderVo();
                if (StrUtil.isEmpty(header.getName())){
                    continue;
                }
                vo.setName(header.getName());
                vo.setValue(header.getValue());
                headerVos.add(vo);
            }
            this.headers=headerVos;

        }
    }
}
